package com.emip.dao.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.emip.bean.WeekStoreBean;
import com.emip.dao.WeekStoreDAO;


@Repository("weekStoreDAO")
public class WeekStoreDAOImpl extends StoreDAO implements WeekStoreDAO {

	
	public List<WeekStoreBean> getByTimespan(int startYear, int startWeekth, 
			int endYear, int endWeekth) {
		return this.getByTimespan(startYear, startWeekth, endYear, endWeekth, null, null);
	}

	@SuppressWarnings("unchecked")
	public List<WeekStoreBean> getByTimespan(int startYear, int startWeekth, 
			int endYear, int endWeekth, String[] items, String siteId) {
		String hql = getWeekHQL(startYear, startWeekth, endYear, endWeekth, items, siteId);
		hql += ", entity.item.id";
		return this.getHibernateTemplate().find(hql);
	}

	public WeekStoreBean merge(WeekStoreBean store) {
		return getHibernateTemplate().merge(store);
	}

	@SuppressWarnings("unchecked")
	public long getCount(int startYear, int startWeekth, int endYear,
			int endWeekth, String[] items, String siteId) {
		String hql = "select distinct entity.year, entity.weekth " + 
			getWeekHQL(startYear, startWeekth, endYear, endWeekth, items, siteId);
		List<Object> objs = getHibernateTemplate().find(hql);
		if (objs != null) return objs.size();
		return 0;
	}

	@SuppressWarnings("unchecked")
	public String getLastDate(String itemId, String siteId) {
		String hql = "select max(w.year) from WeekStoreBean w where 1 = 1";
		if (itemId != null) {
			hql += " and w.item.id = '" + itemId + "'";
		}
		if (siteId != null) {
			hql += " and w.site.id = '" + siteId + "'";
		}
		List<Object> list = getHibernateTemplate().find(hql);
		if (list != null && list.size() > 0 && list.get(0) != null) {
			String year =  list.get(0).toString();
			hql = "select max(w.weekth) from WeekStoreBean w where w.year = '" + year + "'";
			if (itemId != null) {
				hql += " and w.item.id = '" + itemId + "'";
			}
			if (siteId != null) {
				hql += " and w.site.id = '" + siteId + "'";
			}
			list = getHibernateTemplate().find(hql);
			if (list != null && list.size() > 0) {
				return year + " " + list.get(0);
			}
		}
		return null;
	}

	public List<WeekStoreBean> getByTimespan(int startYear, int startWeekth,
			int endYear, int endWeekth, String[] items, String siteId,
			int page, int size) {
		return getThPage(WeekStoreBean.class, "weekth", startYear, startWeekth, endYear, endWeekth, items, siteId, page, size);
	}
	
	public Object[] getStatistics(int startYear, int startWeekth,
			int endYear, int endWeekth, String itemIds, String siteId) {
		long count = this.getCount(startYear, startWeekth, endYear, endWeekth, new String[]{itemIds}, siteId);
		if (count == 0) return null;
		String[] result = this.getThStatistic("Week", "weekth", startYear, startWeekth, endYear, endWeekth, new String[]{itemIds}, siteId);
		result[3] = Integer.parseInt(result[3])*100/count + "%";
		return result;
	}

	public WeekStoreBean findByWeekth(String year, String weekth,
			String siteId, String itemId) {
		int iyear = Integer.parseInt(year);
		int iweekth = Integer.parseInt(weekth);
		List<WeekStoreBean> weeks = this.getByTimespan(iyear, iweekth, iyear, iweekth, new String[]{itemId}, siteId);
		if (weeks != null && weeks.size() > 0) {
			return weeks.get(0);
		}
		return null;
	}
}
